<?php

class UserController{

	function userAdd(){//添加用户
		$model=new RoleModel();
		$data=$model->findAll();
		//print_r($data);
		include(ROOT.'view/admin/tpl/user_add.html');
	}


	function userAddAction(){//处理addUser()函数提交的post
		$user1=new UserModel();
		$role_id=$_POST['role_id']+0;
		
		$data=array();
		$data1=array();
		
	//先对POST上来的数据自动过滤
			$data=$user1->cut($_POST);

	//rint_r($data);exit;

	//再对过滤后的$data自动补充
			$data=$user1->fillFields($data);
	//print_r($data);exit;
			$data['paswd']=md5($data['paswd']);
	$message = new MessageTool();
	if($user1->add($data)){//用户表插入成功
		$id=$user1->insertId();
		
		$m=new RoleUserModel();
		$data1['role_id']=$role_id;
		$data1['user_id']=$id;
		//print_r($data1);
	   if(!$m->add($data1)){//添加到role_user表
	   	   $msg = '用户添加失败';
		   $message->prompt($msg,"./index.php?c=user&a=userList");
		   exit;
	   }
	   $msg = '用户添加成功';
	   $message->prompt($msg,"./index.php?c=user&a=userAdd");
	   exit;
	}else{
		
	   $msg = '用户添加失败';
	   $message->prompt($msg,"./index.php?c=user&a=userList");
	   exit;
	}

	$data=$user1->findAllRoles();

	include(ROOT.'view/admin/tpl/user_add.html');
}

function userList(){//列出所有用户

	$model=new UserModel();
	$data=$model->findAllRoles();
	//print_r($data);
	
	include(ROOT.'view/admin/tpl/user_list.html');
	
}

public function userDel(){//根据主键id删除一个用户

	$uid=$_GET['id']+0;

	$modelu=new UserModel();

	if($modelu->delete($uid)){
		//删除role_user表中所含$uid的行
	$modelru=new RoleUserModel();
	$modelru->deleteRole($uid);
		echo "删除数据成功！";
	}else{
		echo "删除数据失败！";
	}
	

	$data=$modelu->findAllRoles();
	include(ROOT.'view/admin/tpl/user_list.html');
}


public function roleAdd(){//添加一个角色，角色独立添加，此处不绑定用户

	include(ROOT.'view/admin/tpl/role_add.html');

}


public function roleAddAction(){//处理roleAdd()函数的post提交

//接收数据
//print_r($_POST);
	$data=array();
	$model=new RoleModel();
	$message = new MessageTool();
	$data=$model->cut($_POST);
	if(!$model->validate($data)){
		//$msg = implode("<br/>",$model->getErr());
		$msg = implode("<br/>",$model->getErr());
		$message->prompt($msg,"./index.php?c=user&a=roleAdd");
		exit;
	}
//print_r($data);
//将$data中所有数据写入数据库
	if($model->add($data)){
		$msg = '填加角色成功';
		$message->prompt($msg,"./index.php?c=user&a=roleList");
		exit;
	}else{
		$msg = '填加角色失败';
		$message->prompt($msg,"./index.php?c=user&a=roleAdd");
		exit;
	}
}

public function roleList(){//列出所有角色，即系统中共有几个角色，与用户无关

	$model=new RoleModel();
	$data=$model->findAll();
	include(ROOT.'view/admin/tpl/role_list.html');
}


function roleUpdate(){//根据用户id修改该用户的角色


	$user_id=$_GET['id']+0;
	
	$modelu=new UserModel();
	$user=$modelu->findOne($user_id);
	$uname=$user['uname'];
	$paswd=$user['paswd'];
	$modelru=new RoleUserModel();
	$roleid=$modelru->findRoleId($user_id);
	$role_id=$roleid['role_id']+0;


	$modelr=new RoleModel();
	$data=$modelr->findAll();

	include(ROOT.'view/admin/tpl/role_update.html');

}


public function roleUpdateAction(){//处理roleUpdate()函数的post提交
	
    $uid=$_POST['uid']+0;
	$role_id=$_POST['role_id']+0;

	$m=new RoleUserModel();
	$message = new MessageTool();
	if($id=$m->updateRole($role_id,$uid)){
		$msg = '角色修改成功';
		$message->prompt($msg,"./index.php?c=user&a=userList");
		exit;
	}else{
		$msg = '角色修改失败';
		$message->prompt($msg,"./index.php?c=user&a=userList");
		exit;
	}

}


public function rolePower(){//获取角色的权限，用树形状展开，复选框选中表示拥有的权限

	$role_id=$_GET['role_id']+0;
	$model1=new RoleModel();
	$role=$model1->findOne($role_id);
	//echo $role['name'];

	$access=new AccessModel();


	$model=new PowerModel();
	$data=$model->findAll();
	$data=$model->getCategoryTree($data,0);

	foreach($data as $v){
		$count=count($access->getAccess($role_id,$v['id']));
		if($count){
			$v['access']=1;
		}else{
			$v['access']=0;

		}

		$data2[]=$v;
	}

	include(ROOT.'view/admin/tpl/role_power.html');
}

public function roleDel(){//根据角色Id删除一个角色

   //删除角色id
	$role_id=$_GET['id']+0;
//echo $fac_id;exit;
	$model=new RoleModel();
	
	$message = new MessageTool();
	if($model->delete($role_id)){
	//删除access表中含有role_id的行
	$access=new AccessModel();
	$res = $access->delete($role_id);
	$msg ="删除数据成功！";
	$message->prompt($msg,"./index.php?c=user&a=roleList");
	exit;	
	}else{
		$msg ="删除数据失败！";
		$message->prompt($msg,"./index.php?c=user&a=roleList");
		exit;
	}

}



public function rolePowerAction(){//设置角色的权限，根据rolePower()界面中的勾选

	$i=$_POST['role_id']+0;
	$model=new AccessModel();
	$model->delete($i);//清空当前角色的所有权限
	//var_dump( $_POST['access']);exit;
	$datas=array();
	$message = new MessageTool();
	if(isset($_POST['access'])){
		foreach($_POST['access'] as $v){
			$tmp=explode('_', $v);
			//var_dump($tmp);
			$datas[]=array(
				'role_id'=>$i,
				'node_id'=>$tmp[0],
				'level'=>$tmp[1]
				);
		}
	}else{
		$msg = "权限修改失败,请重新修改！";
		$message->prompt($msg,"./index.php?c=user&a=roleList");
		exit;
	}

    
	if($model->addAll($datas)){
		$msg = "权限修改成功";
		$message->prompt($msg,"./index.php?c=user&a=roleList");
		exit;
	}else{
		$msg = "权限修改失败,请重新修改";
		$message->prompt($msg,"./index.php?c=user&a=roleList");
		exit;
	}
}


public function powerAdd(){//权限添加，即添加模块和操作

	$model=new PowerModel();
	$data1=$model->findAll();
	$data=$model->getCategoryTree($data1,0);
	include(ROOT.'view/admin/tpl/power_add.html');
}

public function powerAddAction(){//处理添加权限powerAdd()的post提交，

	$model=new PowerModel();

	$data=array();
	
//先对POST上来的数据自动过滤
	$data=$model->cut($_POST);


/****
编辑栏目时，不能选择自身或其子分类作为其上级分类，

方法：我们为A设定一个新的父栏目时，设为N，
可先查N的家谱树，N的家谱树里，如果有A，则
子孙差辈分了
****/

/*
//查找新父栏目的家谱树
$familytree=$model->getFamilyTree($data['pid']);
//判断自身是否在新父栏目的家谱树里面
$flag=true;
foreach($familytree as $v){
	if($v['id']==$id){
		$flag=false;
		break;
	}
}

if(!$flag){
	//ehco $id.'是'.$paid.'的祖先';
	echo '父栏目选取错误';
	exit;
}



exit;
*/
//再对过滤后的$data自动补充
$data=$model->fillFields($data);

$message = new MessageTool();
if($id=$model->add($data)){
	$msg = "权限添加成功";
	$message->prompt($msg,"./index.php?c=user&a=powerAdd");
	exit;
}else{
	$msg = "权限添加失败";
	$message->prompt($msg,"./index.php?c=user&a=powerAdd");
	exit;
}

}

public function powerList(){//列出所有权限,可以用分页类分页显示

	$model=new PowerModel();
	$data1=$model->findAll();
	$total=count($data1);


//$pageTool=new PageTool($total,$page,$perPage);
//$pageNav=$pageTool->show();
//$data=$faculty->findPerPage($offset,2);

	$data=$model->getCategoryTree($data1,0);

	include(ROOT.'view/admin/tpl/power_list.html');
}

public function powerDel(){//根据权限的id删除权限

	$id=$_GET['id']+0;
//echo $fac_id;exit;
	$model=new PowerModel();
	$message = new MessageTool();
	if($model->delete($id)){
		$msg = "删除数据成功！";
		$message->prompt($msg,"./index.php?c=user&a=powerList");
		exit;
	}else{
		$msg = "删除数据失败！";
		$message->prompt($msg,"./index.php?c=user&a=powerList");
		exit;
	}

}

  public function roleUserUpdateAction(){//角色-用户表同时更新
  	$model=new User1Model();
	$role_id=$_POST['role_id']+0;
	echo $role_id;
	$data=array();
	$data1=array();
	//print_r($_POST);exit;
	//先对POST上来的数据自动过滤
	$data=$model->cut($_POST);

	//rint_r($data);exit;

	//再对过滤后的$data自动补充
	$data=$model->fillFields($data);
	//print_r($data);exit;
	$data['paswd']=md5($data['paswd']);
    $message = new MessageTool();
	if($id=$model->add($data)){
		echo $id=$model->insertId();
		$m=new RoleUserModel();
	   $data1['role_id']=$role_id+0;
	   $data1['uid']=$id+0;
	   $m->add($data1);//添加到role_user表
		$msg = "用户修改成功";
		$message->prompt($msg,"./index.php?c=user&a=powerList");
		exit;
	}else{
		
		$msg = "用户修改失败";
		$message->prompt($msg,"./index.php?c=user&a=powerList");
		exit;
	}

	$data=$model->findAll();

	include(ROOT.'view/admin/tpl/user_list.html');
	  }
	/*
	*ajax获取权限名称制作面包屑
	**/
	function getNodeName(){
		$power = new PowerModel();
		$url = isset($_GET['href'])?$_GET['href']:"./index.php";
		$parse = parse_url($url);
		$param = explode("=",$parse['query']);
		$param1 = explode("&", $param[1]);
		$c = $power->getTitle($param1[0])?$power->getTitle($param1[0]):"首页";
		$a = $power->getTitle($param[2])?$power->getTitle($param[2]):"";
		$data = array();
		$data['module'] = $c;
		$data['method'] = $a;
		echo json_encode($data);
	}
}
